#include <bits/stdc++.h>
#define ll long long
using namespace std;

template < typename T >
inline void read(T &x)
{
    x = 0;
    bool flg = 0;
    char c;
    while ((c = getchar()) < 48 || c > 57)
        if (c == '-') flg = 1;
    do
    {
        x = (x << 1) + (x << 3) + (c ^ 48);
    } while ((c = getchar()) > 47 && c < 58);
    if (flg) x = -x;
}

int OUTPUT[45];

template < typename T >
inline void write(T x)
{
    if (x < 0)
    {
        putchar('-');
        x = -x;
    }
    int len = 0;
    do
    {
        OUTPUT[++len] = ((x % 10) | 48);
        x /= 10;
    } while (x);
    while (len)
        putchar(OUTPUT[len--]);
}

template < typename T >
inline void writesp(T x)
{
    write(x);
    putchar(32);
}

template < typename T >
inline void writeln(T x)
{
    write(x);
    putchar(10);
}

const int N = (1 << 17) + 5;
int a0[N], a[N], b[5], c[N], d[18][N]/*, mi[21][N]*/;
ll ans[N], s[18][N][18];
// bool vis[N];
char str[N];

int main()
{
    freopen("arena.in", "r", stdin);
    freopen("arena.out", "w", stdout);
    int n, m;
    read(n);
    read(m);
    for (int i = 1; i <= n; ++i)
        read(a0[i]);
    for (int i = 1; i <= m; ++i)
        read(c[i]);
    int K = 0;
    while ((1 << K) < n)
        ++K;
    for (int i = 1; i <= K; ++i)
    {
        int num = (1 << (K - i));
        scanf("%s", str + 1);
        for (int j = 1; j <= num; ++j)
            d[i][j] = (str[j] ^ 48);
            // read(d[i][j]);
    }
    int T;
    read(T);
    while (T--)
    {
        read(b[0]);
        read(b[1]);
        read(b[2]);
        read(b[3]);
        for (int i = 1; i <= n; ++i)
            a[i] = (a0[i] ^ b[i & 3]);
        /*for (int i = 1; i <= K; ++i)
            for (int j = (1 << (K - i)); j; --j)
                mi[i][j] = i;
        // mi[K][1] = K;
        for (int i = K; i > 1; --i)
        {
            int num = (1 << (K - i));
            for (int j = 1; j <= num; ++j)
                if (d[i][j]) mi[i - 1][j << 1] = mi[i][j];
                else mi[i - 1][(j << 1) - 1] = mi[i][j];
        }
        for (int i = 1; i <= (1 << (K - 1)); ++i)
            if (d[1][i]) ;*/
        for (int i = 1; i <= m; ++i)
        {
            ans[c[i]] = 0;
            // vis[c[i]] = 0;
        }
        // for (int i = 1; i <= K; ++i)
        //     for (int j = 1; j <= (1 << (K - i)); ++j)
        //         for (int k = 0; k <= K; ++k)
        //             s[i][j][k] = 0;
        ans[1] = 1;
        for (int i = 1; i <= K; ++i)
        {
            for (int j = 1; j <= i; ++j)
                for (int k = 1; k <= (1 << (i - j)); ++k)
                    for (int l = 0; l <= K; ++l)
                        s[j][k][l] = 0;
            int lft = (1 << (i - 1)) + 1, rgh = (1 << i);
            for (int j = 1; j <= (1 << (i - 1)); ++j)
                if (d[1][j])
                {
                    int num = (j << 1);
                    if (num <= lft)
                    {
                        if (a[num] >= 1) s[1][j][min(a[num], K)] += num;
                        else s[1][j][min(a[num - 1], K)] += num - 1;
                    }
                    else
                    {
                        s[1][j][K] += num;
                        if (num - 1 <= lft) s[1][j][min(a[num - 1], K)] += num - 1;
                        else s[1][j][K] += num - 1;
                    }
                }
                else
                {
                    int num = (j << 1) - 1;
                    if (num <= lft)
                    {
                        if (a[num] >= 1) s[1][j][min(a[num], K)] += num;
                        else
                        {
                            if (num + 1 <= lft) s[1][j][min(a[num + 1], K)] += num + 1;
                            else s[1][j][K] += num + 1;
                        }
                    }
                    else
                    {
                        // num > lft, num + 1 > lft
                        s[1][j][K] += num;
                        s[1][j][K] += num + 1;
                    }
                }
            for (int j = 2; j <= i; ++j)
                for (int k = 1; k <= (1 << (i - j)); ++k)
                    if (d[j][k])
                    {
                       int num = (k << 1);
                        ll tmp = 0;
                        for (int l = 0; l < j; ++l)
                            tmp += s[j - 1][num][l];
                        if (tmp)
                        {
                            for (int l = 0; l <= K; ++l)
                                s[j][k][l] += s[j - 1][num - 1][l];
                        }
                        for (int l = j; l <= K; ++l)
                            s[j][k][l] += s[j - 1][num][l];
                        // if (lft == 5 && rgh == 8 && j == i)
                        // {
                        //     writesp(s[j - 1][k << 1][K]), writesp(s[1][1][K]), puts("abc");
                        // }
                     }
                    else
                    {
                        int num = (k << 1) - 1;
                        ll tmp = 0;
                        for (int l = 0; l < j; ++l)
                            tmp += s[j - 1][num][l];
                        if (tmp)
                        {
                            for (int l = 0; l <= K; ++l)
                                s[j][k][l] += s[j - 1][num + 1][l];
                        }
                        for (int l = j; l <= K; ++l)
                            s[j][k][l] += s[j - 1][num][l];
                    }
            for (int j = 0; j <= K; ++j)
                ans[lft] += s[i][1][j];
            // if (lft == 5 && rgh == 8)
            // {
            //     writesp(s[2][1][K]), writesp(s[2][2][K]), writesp(s[i][1][K]), writesp(ans[5]), puts("K");
            // }
            while (++lft <= min(rgh, n))
            {
                int x = (lft + 1) >> 1;
                if (d[1][x])
                {
                    int num = (x << 1);
                    if (num <= lft)
                    {
                        if (a[num] >= 1) s[1][x][min(a[num], K)] += num;
                        else s[1][x][min(a[num - 1], K)] += num - 1;
                    }
                    else
                    {
                        s[1][x][K] += num;
                        if (num - 1 <= lft) s[1][x][min(a[num - 1], K)] += num - 1;
                        else s[1][x][K] += num - 1;
                    }
                }
                else
                {
                    int num = (x << 1) - 1;
                    if (num <= lft)
                    {
                        if (a[num] >= 1) s[1][x][min(a[num], K)] += num;
                        else
                        {
                            if (num + 1 <= lft) s[1][x][min(a[num + 1], K)] += num + 1;
                            else s[1][x][K] += num + 1;
                        }
                    }
                    else
                    {
                        // num > lft, num + 1 > lft
                        s[1][x][K] += num;
                        s[1][x][K] += num + 1;
                    }
                }
                for (int j = 2; j <= i; ++j)
                {
                    int k = (x + 1) >> 1;
                    if (d[j][k])
                    {
                        int num = (k << 1);
                        ll tmp = 0;
                        for (int l = 0; l < j; ++l)
                            tmp += s[j - 1][num][l];
                        if (tmp)
                        {
                            for (int l = 0; l <= K; ++l)
                                s[j][k][l] += s[j - 1][num - 1][l];
                        }
                        for (int l = j; l <= K; ++l)
                            s[j][k][l] += s[j - 1][num][l];
                    }
                    else
                    {
                        int num = (k << 1) - 1;
                        ll tmp = 0;
                        for (int l = 0; l < j; ++l)
                            tmp += s[j - 1][num][l];
                        if (tmp)
                        {
                            for (int l = 0; l <= K; ++l)
                                s[j][k][l] += s[j - 1][num + 1][l];
                        }
                        for (int l = j; l <= K; ++l)
                            s[j][k][l] += s[j - 1][num][l];
                    }
                    x = k;
                }
                for (int j = 0; j <= K; ++j)
                    ans[lft] += s[i][1][j];
            }
        }
        ll res = 0;
        for (int i = 1; i <= m; ++i)
        {
            // if (!vis[c[i]])
            // {
            //     vis[c[i]] = 1;
                
            // }
            // writesp(i), writesp(c[i]), puts("c");
            res ^= ((ll)i * ans[c[i]]);
        }
        writeln(res);
    }
    return 0;
}
